<%@page import="model.Attempt"%>
<!DOCTYPE html>
<html>
    <head>
        <title>IEPM</title>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <%@include file = "global/include.jsp" %>

        <script type="text/javascript">
            window.history.forward();
            function noBack(){ window.history.forward(); }
            
            $(function(){
                // Button
                $("#divButton, #linkButton, #submitButton, #inputButton").button();
            }); 
            
            function validateEmail(elementValue){  
                var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
                return emailPattern.test(elementValue);  
            }
            
            function backToHome(){ 
                window.open("index.jsp","_self");
            }
            
            
            $(function(){
                $('#registrationForm').form({ 
                    onSubmit:function(){
                        var name = $('#name').val();
                        var company_name = $('#company_name').val();
                        var title = $('#title').val();
                        var company_size = $("#company_size :selected").val();
                        var it_size = $("#it_size :selected").val();
                        var email = $('#email').val();
                        var cemail = $('#cemail').val();

                        <% String reg = (String) session.getAttribute("regFlag");%>
                        <% if (!reg.equals("true")) {%>
                            if (company_size == "" || it_size == "") {
                                $('#failedSizeDialog').dialog('open');
                                return false;
                            } else if (email != "") {
                                if (!validateEmail(email)) {
                                    $('#failedInvalidEmailDialog').dialog('open');
                                    return false;
                                } else if (email != cemail) {
                                    $('#failedEmailDialog').dialog('open');
                                    return false;
                                } else {
                                    $('#loadingDialog').dialog('open');
                                    return true;
                                }
                            } else {
                                $('#loadingDialog').dialog('open');
                                return true;
                            }
                        <% } else {%>
                            if (name == "" || company_name == "" || title == "" || company_size == "" || it_size == "" || email == "") {
                                $('#failedAllFieldDialog').dialog('open');
                                return false;
                            } else if (!validateEmail(email)) {
                                $('#failedInvalidEmailDialog').dialog('open');
                                return false;
                            } else if (email != cemail) {
                                $('#failedEmailDialog').dialog('open');
                                return false;
                            } else {
                                $('#loadingDialog').dialog('open');
                                return true;
                            }
                        <% }%>
                        },
                        success:function(data){
                            var obj = jQuery.parseJSON(data); 
                            $('#loadingDialog').dialog('close');
                        
                            if(obj=="success"){
                                $('#registrationForm').form('clear');
                                <% if (!reg.equals("true")) {%>
                                    //window.open("index.jsp","_self");
                                    
                                   $('#centerContent2').html('<br/><input onclick="backToHome()" type="button" value="Click To Return To Home Page."/><br/><br/>');
                                   
                                    window.open("FrontEndServlet?action=getReport&getPDF=<%=((Attempt) session.getAttribute("Attempts")).getLink()%>","_blank");
                                  
                                <% } else {%>
                                    $('#successDialog').dialog('open');
                                <% }%>
                            }else if(obj=="email-failed"){
                                $('#failedSendEmailDialog').dialog('open');
                            }else{
                                $('#failedDialog').dialog('open');
                            }
                        }
                    });
                });
            
                $(document).ready(function(){
                    $('#successDialog').dialog({
                        autoOpen: false,
                        width: 400,
                        height: 200,
                        buttons: {
                            Ok: function() {
                                window.open("index.jsp","_self");
                                //$( this ).dialog( "close" );
                            }
                        },
                        modal: true
                    });
                
                    $('#failedDialog').dialog({
                        autoOpen: false,
                        width: 400,
                        height: 200,
                        buttons: {
                            Ok: function() {
                                // window.open("index.jsp","_self");
                                $( this ).dialog( "close" );
                            }
                        },
                        modal: true
                    });
                
                    $('#loadingDialog').dialog({
                        autoOpen: false,
                        width: 400,
                        height: 200,
                        modal: true
                    });
                
                    $('#failedEmailDialog').dialog({
                        autoOpen: false,
                        width: 400,
                        height: 200,
                        buttons: {
                            Ok: function() {
                                // window.open("index.jsp","_self");
                                $( this ).dialog( "close" );
                            }
                        },
                        modal: true
                    });
                
                    $('#failedAllFieldDialog').dialog({
                        autoOpen: false,
                        width: 400,
                        height: 200,
                        buttons: {
                            Ok: function() {
                                // window.open("index.jsp","_self");
                                $( this ).dialog( "close" );
                            }
                        },
                        modal: true
                    });
                
                    $('#failedSendEmailDialog').dialog({
                        autoOpen: false,
                        width: 400,
                        height: 250,
                        buttons: {
                            Ok: function() {
                                // window.open("index.jsp","_self");
                                $( this ).dialog( "close" );
                            }
                        },
                        modal: true
                    });
                
                    $('#failedSizeDialog').dialog({
                        autoOpen: false,
                        width: 400,
                        height: 250,
                        buttons: {
                            Ok: function() {
                                // window.open("index.jsp","_self");
                                $( this ).dialog( "close" );
                            }
                        },
                        modal: true
                    });
                
                    $('#failedInvalidEmailDialog').dialog({
                        autoOpen: false,
                        width: 400,
                        height: 250,
                        buttons: {
                            Ok: function() {
                                // window.open("index.jsp","_self");
                                $( this ).dialog( "close" );
                            }
                        },
                        modal: true
                    });
                });
        </script>
    </head>
    <body onload="noBack();" onpageshow="if (event.persisted) noBack();" onunload="">
        <div id="centerColumn" class="ui-widget ui-widget-content ui-corner-all">
            <%@include file = "global/header.jsp" %>
            <!--//Your Content//-->
            <h2 title="Registration" class="reflected">Registration</h2>
            <br/>
            <p>Augur et fulgente decorus arcu Phoebus acceptusque novem Camenis, qui salutari levat arte fessos corporis artus, si Palatinas videt aequos aras remque Romanam Latiumque felix alterum in lustrum meliusque semper prorogat aevom, quaeque Aventinum tenet Algidumque, quindecim Diana preces virorum curat et votis puerorum amicas adplicat auris.</p>
            <div id="centerContent2" class="ui-widget-content ui-corner-all">
                <form action="FrontEndServlet?action=registration" method="post" id="registrationForm">
                    <table border="0" cellpadding="5" >
                        <tbody>
                            <tr>
                                <td align="right" ><label for="name">Name:</label></td>
                                <td align="left"><input style="width:200px;" size="100" name="name" type="text"/>&nbsp;<font size="2">(100 Char)</font></td>
                            </tr>
                            <tr>
                                <td align="right"><label for="company_name">Company Name:</label></td>
                                <td align="left"><input style="width:200px;" size="100" name="company_name" type="text"/>&nbsp;<font size="2">(100 Char)</font></td>
                            </tr>
                            <tr>
                                <td align="right"><label for="title">Title:</label></td>
                                <td align="left"><input style="width:200px;" size="50" name="title" type="text"/>&nbsp;<font size="2">(50 Char)</font></td>
                            </tr>
                            <tr>
                                <td align="right"><label for="company_size">Company Size:</label></td>
                                <td align="left"> 
                                    <select id="company_size" name="company_size" style="width:200px;">
                                        <option value=""></option> 
                                        <option value="0  to 25">0  to 25</option> 
                                        <option value="26 to 50">26 to 50</option> 
                                        <option value="51 to 75">51 to 75</option> 
                                        <option value="76 and more">76 and more</option>   
                                    </select>  
                                </td>
                            </tr>
                            <tr>
                                <td align="right"><label for="it_size">IT Department Size:</label></td>
                                <td align="left">
                                    <select id="it_size" name="it_size" style="width:200px;"> 
                                        <option value=""></option> 
                                        <option value="0">0</option> 
                                        <option value="1 to 3">1 to 3</option> 
                                        <option value="4 to 6">4 to 6</option> 
                                        <option value="6 and more">6 and more</option>  
                                    </select>  
                                </td>
                            </tr>
                            <tr>
                                <td align="right"><label for="email">Email:</label></td>
                                <td align="left"><input style="width:200px;" name="email" id="email" type="text"/>
                                </td>
                            </tr>
                            <tr>
                                <td align="right"><label for="email">Confirm Email:</label></td>
                                <td align="left"><input style="width:200px;" name="cemail" id="cemail" type="text"/>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                    <input id="inputButton" type="submit" value="Submit"/>
                    <br/>
                </form>
            </div>
            <br/>
            <%@include file = "global/footer.jsp" %>
        </div>
        <!--//end #centerColumn//-->
        <div id="loadingDialog" style=" text-align: center" title="Generating Report">
            <img src="css/img/loading.gif" alt="loading"/>   
        </div>
        <div id="successDialog" style=" text-align: center" title="Generating Report">
            <p>Report(s) successfully sent to your email! Click "OK" to go back to home page.</p> 
        </div>
        <div id="failedDialog" style=" text-align: center" title="Generating Report">
            <p>Sending report(s) failed! Please enter a valid email.</p> 
        </div>
        <div id="failedSendEmailDialog" style=" text-align: center" title="Generating Report">
            <p>Email server is down!<br/>Your new report is unable to sent to you at this moment! Please try to retrieve it again later.</p> 
        </div>
        <div id="failedInvalidEmailDialog" style=" text-align: center" title="Generating Report">
            <p>Invalid email</p> 
        </div>
        <div id="failedEmailDialog" style=" text-align: center" title="Generating Report">
            <p>Email and Confirm email does not match</p> 
        </div>
        <div id="failedAllFieldDialog" style=" text-align: center" title="Generating Report">
            <p>All fields are required</p> 
        </div>
        <div id="failedSizeDialog" style=" text-align: center" title="Generating Report">
            <p>Company size and IT Department Size are required</p> 
        </div>
    </body>
</html>
